package com.yxe.controller;


import com.yxe.api.CommonResult;
import com.yxe.pojo.Pay;
import com.yxe.service.ExpenseService;
import com.yxe.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/pay")
@Slf4j
@Api(tags = "费用明细")
public class PayController {

    @Autowired
    private ExpenseService expenseService;

    @Autowired
    private PayService payService;

    /**
     * 充值费用
     * @param
     * @return
     */
    @PostMapping("payExpense")
    @ApiOperation("充值费用")
    @ResponseBody
    public CommonResult updateExpense(@RequestParam("payType") Integer payType,
                                      @RequestParam("payMoney") Float payMoney,
                                      @RequestParam("userId") Integer userId) {

        expenseService.updateExpense(payType, payMoney, userId);

        payService.insert(payType, payMoney, userId);

        return CommonResult.success("充值成功");
    }

    /**
     * 所有费用账单查询
     * @param
     * @return
     */
    @PostMapping("selectExpense")
    @ApiOperation("所有费用账单查询")
    @ResponseBody
    public CommonResult<List<Pay>> selectExpense(@RequestParam("userId") Integer userId){

        List<Pay> payList = payService.selectAllById(userId);
        if (payList.isEmpty()) {
            return CommonResult.failed("数据为空");
        }else {
            return CommonResult.success(payList);
        }
    }

    /**
     * 单类费用账单查询
     * @param
     * @return
     */
    @PostMapping("selectOneExpense")
    @ApiOperation("单类费用账单查询")
    @ResponseBody
    public CommonResult<List<Pay>> selectOneExpense(@RequestParam("userId") Integer userId,
                                                    @RequestParam("payType") Integer payType){

        List<Pay> payList = payService.selectAllByType(userId, payType);
        if (payList.isEmpty()) {
            return CommonResult.failed("数据为空");
        }else {
            return CommonResult.success(payList);
        }
    }
}

